function galaxytool_coordinate_input(value, constraints) {
	var i, j, subcheck, requests, request;
	
	value = value.replace(/\s/g,"");
	if (value == "") return true;
	
	requests = value.split(";");
	
	for (i=0;i<requests.length;i++) {
		request = requests[i].split(":");
		if (request.length > 3) return false;
		for (j=0;j<request.length;j++) {
			if (request[j] == "") return false;
			subcheck = request[j].replace(/\d+(\-(?:\d+))?/,"");
			if (subcheck != "") return false;
		}
	}
	
    return true;
}

/*
 * Validate popup window coordinate entries and attach them to given dijit textbox
 */
function galaxytool_apply_coordinates(dijit_textbox) {
	var fields = new Array("galaxy1","galaxy2","system1","system2","planet1","planet2");
	var i, error;

	// 1. validate that all fields are valid
	for (i=0; i<fields.length; i++) {
		if (dijit.byId(fields[i]).get("state") != "") {
			return;
		}
	}

	// 2. validate that the fields are valid alltogether
	var galaxy1 = (dijit.byId("galaxy1").get("value") != "") ? parseInt(dijit.byId("galaxy1").get("value")) : "";
	var galaxy2 = (dijit.byId("galaxy2").get("value") != "") ? parseInt(dijit.byId("galaxy2").get("value")) : "";
	var system1 = (dijit.byId("system1").get("value") != "") ? parseInt(dijit.byId("system1").get("value")) : "";
	var system2 = (dijit.byId("system2").get("value") != "") ? parseInt(dijit.byId("system2").get("value")) : "";
	var planet1 = (dijit.byId("planet1").get("value") != "") ? parseInt(dijit.byId("planet1").get("value")) : "";
	var planet2 = (dijit.byId("planet2").get("value") != "") ? parseInt(dijit.byId("planet2").get("value")) : "";

	if ((system1=="") && (system2 != "" || planet1 != "" || planet1 != "")) {
		dijit.byId("system1").set("state","Error");
		error=true;
	}
	if (planet1=="" && planet2 != "") {
		dijit.byId("planet1").set("state","Error");
		error=true;
	}
	if (error) return;

	if (galaxy1 < 1) { dijit.byId("galaxy1").set("state","Error"); error=true; }
	if (system1 != "" && system1 < 1) { dijit.byId("system1").set("state","Error"); error=true; }
	if (planet1 != "" && planet1 < 1) { dijit.byId("planet1").set("state","Error"); error=true; }
	if (error) return;
	
	if (galaxy2 < galaxy1 && galaxy2 != "") { dijit.byId("galaxy2").set("state","Error"); error = true; }
	if (system2 < system1 && system2 != "") { dijit.byId("system2").set("state","Error"); error = true; }
	if (planet2 < planet1 && planet2 != "") { dijit.byId("planet2").set("state","Error"); error = true; }
	if (error) return;

	// 3. apply values to textbox
	var coordinates = dijit.byId(dijit_textbox).get("value");
	var new_coordinates = galaxy1;
	if (galaxy2 != "" && galaxy2 != galaxy1) new_coordinates += "-"+galaxy2;
	if (system1 != "") {
		new_coordinates += ":"+system1;
		if (system2 != "" && system2 != system1) new_coordinates += "-"+system2;
		if (planet1 != "") {
			new_coordinates += ":"+planet1;
			if (planet2 != "" && planet2 != planet1) new_coordinates += "-"+planet2;
		}
	}
	if (coordinates != "") {
		coordinates += ";"+new_coordinates;
	} else {
		coordinates = new_coordinates;
	}
	dijit.byId(dijit_textbox).set("value",coordinates);

	// 4. reset values
	for (i=0; i<fields.length; i++) {
		dijit.byId(fields[i]).reset();
	}
}

function validateEmail(email_string){  
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	return emailPattern.test(email_string);  
} 




